package com.app.shell.view.mediators
{
	import com.app.common.GlobalConstants;
	import com.app.shell.message.INotification;
	import com.app.shell.message.ShellMessage;
	import com.app.shell.model.ActiveWidgetProxy;
	import com.app.shell.view.components.LoggerHolderView;
	
	import mx.core.IVisualElement;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	
	public class LoggerHolderMediator {

		[MessageDispatcher] public var dispatcher:Function;
		
		[Bindable] 
		public var view:LoggerHolderView;

		[Inject]
		[Bindable] public var proxy:ActiveWidgetProxy;

		[Observe]
		public function mediateView(view:LoggerHolderView):void {
			trace("Mediator.mediateView() called");
			
			this.view = view;
			
			if (view != null) {
				if (view.initialized) {
					viewInitialized();
				} else {
					view.addEventListener(FlexEvent.CREATION_COMPLETE, componentInitialized);
				}
			}
		}
		
		protected function componentInitialized(e:Event = null):void {
			trace("Mediator.componentInitialized(): View fully initialized, safe to interact");
			viewInitialized();
		}
		
		public function viewInitialized():void {
			trace("Mediator.init(): Initializing view");
		}

		[Observe(phase="postDestroy")]
		public function viewDestroyed(view:LoggerHolderView):void {

		}		

		[Init]
		public function init() : void {
			// Create the logger for application
			dispatcher(new ShellMessage(GlobalConstants.CREATE_LOGGER, GlobalConstants.MODULE_LOGGER));
		}
		
		[MessageHandler(selector='createLoggerComplete')]
		public function respondToCreateLoggerComplete(message:INotification):void
		{
			view.addElement(proxy.getWidget((message as ShellMessage).note as String) as IVisualElement);
		}
		
		[MessageHandler(selector='shutdownLoggerComplete')]
		public function respondToShutdownLoggerComplete(message:INotification):void
		{
			view.removeElement(proxy.getWidget((message as ShellMessage).note as String) as IVisualElement);
			dispatcher(new ShellMessage(GlobalConstants.REMOVE_LOGGER_COMPLETE, GlobalConstants.MODULE_LOGGER));
		}
	
	}
}